package manager.pay;

import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.SQLException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.protocol.HttpRequestHandlerMapper;
import org.springframework.beans.BeansException;

import com.fasterxml.jackson.core.JsonProcessingException;

import database.DBService;
import database.pojo.UserRechargeInfo;
import manager.connection.ClientConnectionManager;
import manager.pay.state.ConsumptionType;
import manager.pay.state.PayState;
import manager.pay.type.PayResultInfo;
import manager.qrcode.GetPayQRCode;
import net.socket.server.response.NTFVipGradeResponse;
import util.MyFactory;

public class HandleNotify {
	
	public void HandleWeixinPayAndZhifubaoPay(String outTradeNo) throws BeansException, IllegalArgumentException, IllegalAccessException, SQLException, ClientProtocolException, URISyntaxException, IOException {
		//如果订单存在并且已经状态已经支付成功，则不处理
		if("".equals(outTradeNo)||MyFactory.context.getBean("GetPayResult", GetPayResult.class).isExitOrderSnAndPaySucc(outTradeNo)) {
				return ;
		}
		PayResultInfo rsp = new PayResultInfo();
		rsp = MyFactory.context.getBean("GetPayResult", GetPayResult.class).outTrafeNoToPayResultInfo(outTradeNo);
		UserRechargeInfo userRechargeInfo =null;
		try {
			userRechargeInfo = MyFactory.context.getBean("GetPayResult", GetPayResult.class).updateUserRecharge(rsp);
		} catch (BeansException | SQLException e) {
			e.printStackTrace();
		}//生成订单,将用户充值记录插入表user_recharge
		int minites = MyFactory.context.getBean("GetPayQRCode", GetPayQRCode.class).getNumFromString(rsp.getRecharge_description());
		
		userRechargeInfo.setRecharge_status(PayState.PAYSUCC);
		
		double beforeAllRecharge,afterAllRecharge;
		if(ClientConnectionManager.allRecharge.containsKey(rsp.getOpenId())) {
			beforeAllRecharge = ClientConnectionManager.allRecharge.get(rsp.getOpenId());
		}else {
			beforeAllRecharge =0;
		}
		afterAllRecharge = beforeAllRecharge +rsp.getPrice();
		//afterAllRecharge = beforeAllRecharge +100;//因为现在充值money都是1分钱，所有默认每次充值都是100元
		ntfVipGrade(beforeAllRecharge,afterAllRecharge,rsp.getMachineId(),rsp.getOpenId());
		System.out.println("afterAllRecharge:"+afterAllRecharge+"rsp.getPrice():"+rsp.getPrice());
		
		ClientConnectionManager.allRecharge.put(rsp.getOpenId(), afterAllRecharge);
		MyFactory.context.getBean("DBService", DBService.class).updateAllRecharge(afterAllRecharge,rsp.getOpenId());
		MyFactory.context.getBean("GetPayResult", GetPayResult.class).ntfPlayerPayStatus(rsp,ConsumptionType.BUYPACKET,minites);//1表示购买套餐
		
		try {
			MyFactory.context.getBean("GetPayResult", GetPayResult.class).insetUserConsume(userRechargeInfo);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		rsp.setOut_trade_no(userRechargeInfo.getRecharge_orderid());
		MyFactory.context.getBean("GetPayResult", GetPayResult.class).ntfPHPServer(rsp);
	}

	

	private void ntfVipGrade(double beforeAllRecharge, double afterAllRecharge, int mid,String openId) throws BeansException, IllegalArgumentException, IllegalAccessException, JsonProcessingException, SQLException {
		NTFVipGradeResponse rsp = new NTFVipGradeResponse();
		int before = (int)beforeAllRecharge;
		int after = (int)afterAllRecharge;
		int beforeVipGrade = allRechargeTOvipGrade(before);
		int afterVipGrade = allRechargeTOvipGrade(after);
		if(afterVipGrade>beforeVipGrade) {
			rsp.setVipGrade(afterVipGrade);
			MyFactory.context.getBean("DBService", DBService.class).updateVipGrade(afterVipGrade,openId);//目前暂时算充一次加300
			MyFactory.context.getBean("GetPayResult", GetPayResult.class).ntfVipGradeChange(rsp,mid);
		}
		
	}



	public int allRechargeTOvipGrade(int AllRecharge) {
		if(AllRecharge <100) {
			return 0;
		}else if (AllRecharge >=100 && AllRecharge <200) {
			return 1;
		}else if (AllRecharge >=200 && AllRecharge <300) {
			return 2;
		}else if (AllRecharge >=300 &&AllRecharge <500) {
			return 3;
		}else if (AllRecharge >=500 &&AllRecharge <1000) {
			return 4;
		}else if (AllRecharge >=1000 &&AllRecharge <1500) {
			return 5;
		}else if (AllRecharge >=1500 &&AllRecharge <2000) {
			return 6;
		}else if (AllRecharge >=2000 &&AllRecharge <2500) {
			return 7;
		}else if (AllRecharge >=2500 &&AllRecharge <3000) {
			return 8;
		}else if (AllRecharge >=3000 &&AllRecharge <3500) {
			return 9;
		}else  {
			return 10;
		}
	}



	//购买k币只算充值，所以不用插入消费表
	public void HandleKweixingAndKZhifubao(String outTradeNo) throws BeansException, SQLException, IllegalArgumentException, IllegalAccessException, ClientProtocolException, URISyntaxException, IOException {
		//如果订单存在并且已经状态已经支付成功，则不处理
		if("".equals(outTradeNo)||MyFactory.context.getBean("GetPayResult", GetPayResult.class).isExitOrderSnAndPaySucc(outTradeNo)) {
				return ;
		}
		//System.out.println("HandleKweixingAndKZhifubao:");
		PayResultInfo rsp = new PayResultInfo();
		rsp = MyFactory.context.getBean("GetPayResult", GetPayResult.class).outTrafeNoToPayResultInfo(outTradeNo);
		try {
			MyFactory.context.getBean("GetPayResult", GetPayResult.class).updateUserRecharge(rsp);//生成订单,将用户充值记录插入表user_recharge
		} catch (BeansException | SQLException e) {
			e.printStackTrace();
		}
		MyFactory.context.getBean("GetPayResult", GetPayResult.class).ntfPlayerPayStatus(rsp,ConsumptionType.BUYKCOIN,0);//2表示充值金币,这个不需要返回时间	
		
		double beforeAllRecharge,afterAllRecharge;
		
		if(ClientConnectionManager.allRecharge.containsKey(rsp.getOpenId())) {
			beforeAllRecharge = ClientConnectionManager.allRecharge.get(rsp.getOpenId());
		}else {
			beforeAllRecharge =0;
		}
		afterAllRecharge = beforeAllRecharge +rsp.getPrice();
		//afterAllRecharge = beforeAllRecharge +100;//因为现在充值money都是1分钱，所有默认每次充值都是100元
		System.out.println("afterAllRecharge:"+afterAllRecharge+"rsp.getPrice():"+rsp.getPrice());
		ClientConnectionManager.allRecharge.put(rsp.getOpenId(), afterAllRecharge);
		ntfVipGrade(beforeAllRecharge,afterAllRecharge,rsp.getMachineId(),rsp.getOpenId());
	
		MyFactory.context.getBean("DBService", DBService.class).updateAllRecharge(afterAllRecharge,rsp.getOpenId());
	
		MyFactory.context.getBean("GetPayResult", GetPayResult.class).ntfPHPServer(rsp);//后一个参数consumptionType
			
		
	}

}
